package com.gmlive.common.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.TextView;

import com.gmlive.common.ui.R;

/**
 * 去掉TextView最后一行的行间距
 *
 * @Author: 姜志敏
 * @CreateDate: 2019-07-09 18:14
 */
@SuppressLint("AppCompatCustomView")
public class LineSpaceExtraTextView extends TextView {
    private Rect mLastLineShowRect;
    private Rect mLastLineActualIndexRect;
    private boolean useFullWidth = false;

    public LineSpaceExtraTextView(Context context) {
        super(context);
        init();
    }

    public LineSpaceExtraTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.LineSpaceExtraTextView);
        useFullWidth = typedArray.getBoolean(R.styleable.LineSpaceExtraTextView_useFullWidth, false);
        typedArray.recycle();
        init();
    }

    public LineSpaceExtraTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //去除显示的最后一行的行间距
        //设置行间距 && 设置MaxLines && 实际行数大于MaxLines时，显示的最后一行会增加行间距
        setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() - calculateExtraSpace());
    }

    private int calculateExtraSpace() {
        int lastRowSpace = 0;
        if (getLineCount() > 0) {
            //实际最后一行
            int actualLastRowIndex = getLineCount() - 1;
            //显示的最后一行
            int lastRowIndex = Math.min(getMaxLines(), getLineCount()) - 1;
            if (lastRowIndex >= 0) {
                Layout layout = getLayout();
                //显示的最后一行文字基线坐标
                int baseline = getLineBounds(lastRowIndex, mLastLineShowRect);
                getLineBounds(actualLastRowIndex, mLastLineActualIndexRect);
                //测量显示的高度（measureHeight）等于TextView实际高度（layout.getHeight()）或者等于实际高度减去不可见部分的高度（mLastLineActualIndexRect.bottom - mLastLineShowRect.bottom）
                if (getMeasuredHeight() == layout.getHeight() - (mLastLineActualIndexRect.bottom - mLastLineShowRect.bottom)) {
                    lastRowSpace = mLastLineShowRect.bottom - (baseline + layout.getPaint().getFontMetricsInt().descent);
                }
            }
        }
        return lastRowSpace;
    }

    private void init() {
        mLastLineShowRect = new Rect();
        mLastLineActualIndexRect = new Rect();
    }

    public void setUseFullWidthText(CharSequence text, boolean useFullWidth) {
        this.useFullWidth = useFullWidth;
        if (useFullWidth) {
            setText(toDBC(text.toString()));
        } else {
            setText(text);
        }
    }


    /**
     * 半角转换为全角
     *
     * @param input
     * @return
     */
    public String toDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }
}
